﻿namespace UFIDA.U9.Cust.SH.SHFastShipBP
{
	using System;
	using System.Collections.Generic;
	using System.Text; 
	using UFSoft.UBF.AopFrame;	
	using UFSoft.UBF.Util.Context;
    using UFIDA.U9.CBO.SCM.Customer;
    using UFIDA.U9.CC.CreditBP.Proxy;
    using UFIDA.U9.CC.CreditSV.Proxy;
    using UFIDA.U9.CC.CreditObjectPolicy;
    using UFIDA.U9.CC.CreditBP;
    using System.Data;
    using UFSoft.UBF.Util.DataAccess;
    using UFIDA.U9.Cust.SH.SHFastShipBP.SHFastShipBP;
    using UFIDA.U9.Base;
    using UFSoft.UBF.Sys.Database;

	/// <summary>
	/// GetCustomerCredit partial 
	/// </summary>	
	public partial class GetCustomerCredit 
	{	
		internal BaseStrategy Select()
		{
			return new GetCustomerCreditImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class GetCustomerCreditImpementStrategy : BaseStrategy
	{
		public GetCustomerCreditImpementStrategy() { }

		public override object Do(object obj)
		{						
			GetCustomerCredit bpObj = (GetCustomerCredit)obj;
			
			//get business operation context is as follows
			//IContext context = ContextManager.Context	
			
			//auto generating code end,underside is user custom code
			//and if you Implement replace this Exception Code...
			//throw new NotImplementedException();
            if (bpObj.CustomerID <= 0) throw new Exception("客户ID传入错误!");
            Customer cu = Customer.Finder.FindByID(bpObj.CustomerID);
            if (cu == null) throw new Exception("客户查找出错！");

            decimal zero = 0;
            if (!IsOrgCreditCheck()) return zero;

            //客户获取信用等级信息BP 
            //默认取双虎组织信用
            //long OrgID = 1001106046291924;
            GetObjectCreditLevelProxy cc1 = new GetObjectCreditLevelProxy();
            cc1.CustomerCode = cu.Code;
            ObjectCreditLevelData dto = cc1.Do();
            if (dto == null || dto.ID <= 0) throw new Exception("获取客户["+cu.Name+"]对象信用等级出错！");

            //根据信用等级获得信用占用情况SV
            ObjectCreditLimitStatSVProxy cc2 = new ObjectCreditLimitStatSVProxy();
            cc2.ObjectCreditLevel = dto.ID;
            CreditLimitRateStatDTOData SVdto = cc2.Do();
            
            //已使用额度
            decimal KHYFS = PubTool.ToDecimal(SVdto.ARBalance.ToString()) + PubTool.ToDecimal(SVdto.BadDebts.ToString())
                + PubTool.ToDecimal(SVdto.Shipmented.ToString()) + PubTool.ToDecimal(SVdto.Shipmenting.ToString()) 
                + PubTool.ToDecimal(SVdto.Ordered.ToString())
                - PubTool.ToDecimal(SVdto.UndoReturnMoney.ToString()) - PubTool.ToDecimal(SVdto.PreGathering.ToString());


            //信用额度
            string XYEDSQL = @"select ISNULL(C.CreditContent_CreditLimit,-1) as XYED FROM CBO_Customer A	
                                left join CC_CreditLevel B on A.CreditLevel2 = B.ID 
                                left join CC_CreditLevelCurrency C on c.CreditLevel = B.ID 
                                left join CBO_Customer as D on A.ID=D.ID	
                                where A.Org={0} and D.Code='{1}'";
            XYEDSQL = string.Format(XYEDSQL, Context.LoginOrg.ID, cu.Code);
            string XYED = "0";
            DataSet TB1 = new DataSet();
            int FH1 = DataAccessor.RunSQL(DataAccessor.GetConn(), XYEDSQL, null, out TB1);
            if (FH1 > 0)
            {
                XYED = TB1.Tables[0].Rows[0]["XYED"].ToString();
            }
            //临时额度
            //string LSEDSQL = "select ISNULL(SUM(CC_CreditLimit),0) as LSED FROM CC_TempCredit as A left join  CC_CreditControlObject as B on A.CreditObject=B.ID left join CBO_Customer as C on B.Customer=C.ID WHERE   A.Org = 1001106046291924  AND A.Status = 3 AND A.Cancel_Canceled = 0  AND CONVERT(date,GETDATE()) >=CONVERT(date,EffectiveDate) AND  CONVERT(date,GETDATE()) <=CONVERT(date,DisableDate)  and B.ObjectStatus = 1  AND B.Organization = 1001106046291924  and C.Code='" + cu.Code + "'";

            string LSEDSQL = @"select ISNULL(SUM(CC_CreditLimit),0) as LSED FROM CC_TempCredit as A 
                                left join  CC_CreditControlObject as B on A.CreditObject=B.ID 
                                left join CBO_Customer as C on B.Customer=C.ID 
                                WHERE   A.Org = 1001106046291924  AND A.Status = 3 AND A.Cancel_Canceled = 0    
                                    and B.ObjectStatus = 1  AND B.Organization = {0} 
                                    and DisableDate>='9999-01-01 00:00:00.000' and C.Code='{1}'";
            LSEDSQL = string.Format(LSEDSQL, Context.LoginOrg.ID, cu.Code);
            string LSED = "0";
            DataSet TB2 = new DataSet();
            int FH2 = DataAccessor.RunSQL(DataAccessor.GetConn(), LSEDSQL, null, out TB2);
            if (FH2 > 0)
            {
                LSED = TB2.Tables[0].Rows[0]["LSED"].ToString();
            }
            //额度余额
            //decimal EDYE = decimal.Parse(XYED) + decimal.Parse(LSED) - KHYFS;
            decimal EDYE = KHYFS - PubTool.ToDecimal(LSED);
            return EDYE;
		}

        private bool IsOrgCreditCheck()
        {
            DataSet dskz = new DataSet();
            string sqlkz = @" SELECT b.Value FROM Base_Profile_Trl A ,BASE_ProfileValue b 
                        WHERE a.ID = b.Profile and  A.Name LIKE '%启用信用控制%' and b.Organization = " + Context.LoginOrg.ID;
            int icountkz = DataAccessor.RunSQL(DatabaseManager.GetCurrentConnection(), sqlkz, null, out dskz);
            if (icountkz > 0)
            {
                if (dskz.Tables[0].Rows[0]["Value"].ToString() == "True")
                {
                    return true;
                }
            }
            return false;
        }

	
	}

	#endregion
	
	
}